<?php

namespace App\Http\Controllers\Agentadmin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{


    public function index(Request $request)
    {
        $id=session('user_id');
        $user_name = \DB::table('user')->where(['user_id'=>trim($id),'status'=>1])->whereIn('type',[1,2])->value('user_name');
        if (!$user_name){
            return back()->withErrors('该代理不存在');
        }
        return view('agentadmin.index.index')->with('user_name',$user_name);
    }

    public function welcome()
    {
        return view('agentadmin.index.welcome');
    }
    //会员列表
    public function memberlist(Request $request){
        $seach=$request->seach;
            $id = session('user_id');
            if ($seach){
                $memberlist = \DB::table('user')->where('pid','like','%'.','.trim($id))->where(function($q1) use($seach){
                    $q1->orWhere('user_name',$seach)
                        ->orWhere('phone',$seach);
                })->paginate(15);
            }else{
                $memberlist = \DB::table('user')->where(['pid'=>trim($id)])->orwhere('pid','like','%'.','.trim($id))->paginate(15);
            }
            $count = \DB::table('user')->where('pid','like','%'.','.trim($id))->count();
            foreach ($memberlist as $k=>$v){
                $v->code = \DB::table('user')->where(['user_id'=>$id])->value('user_name');
            }
            $type = \DB::table('user')->where(['user_id'=>$id])->value('type');
        return view('agentadmin.index.memberlist',compact('memberlist','count','type'));
    }


    //成为代理
    public function becomeagent($id){
        $beagent = \DB::table('user')->where(['user_id'=>$id])->update(['type'=>2]);
        if(!$beagent){
            $array = array('msg'=>'修改失败!','status'=>'false');
            return json_encode($array);
        }else{
            $array = array('msg'=>'修改成功!','status'=>'true');
            return json_encode($array);
        }
    }
}
